﻿namespace NKernel.Core.Impl
{
    using System.Collections.Generic;

    using Provider;

    /// <summary>
    /// Responsible for instantiating the component by using the component descriptors
    /// </summary>
    internal class ComponentDescriptorInstantiator
    {
        #region Fields

        private readonly IKernelProvider kernelProvider;

        #endregion Fields

        #region Constructors

        public ComponentDescriptorInstantiator(IKernelProvider kernelProvider)
        {
            this.kernelProvider = kernelProvider;
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Instantiates the specified component descriptors.
        /// </summary>
        /// <param name="componentDescriptors">The component descriptors.</param>
        /// <param name="kernel">The kernel.</param>
        /// <returns></returns>
        internal IList<IComponentObject> Instantiate(IList<ComponentDescriptor> componentDescriptors, IKernel kernel)
        {
            IList<IComponentObject> componentObjects = null;

            if (componentDescriptors.Count == 0)
            {
                //do nothing
            }
            else
            {
                componentObjects = kernelProvider.AssembleComponents(componentDescriptors, kernel);
            }

            return componentObjects ?? new List<IComponentObject>();
        }

        #endregion Methods
    }
}